#include "writeWorld.hpp"
#include "Log.hpp"
#include <algorithm>
using namespace std;
using namespace m3g;

/**
*
*/
unsigned int writeWorld (M3GWriter* writer, unsigned int camera_index, unsigned int background_index, std::vector<unsigned int>& child_indices)
{
	int child_count = 0;
    for (int i = 0; i < (int)child_indices.size(); i++) {
        if (child_indices[i] > 0) {
           child_count++;
        }
    }

	M3GObject3DStruct      obj;
	M3GTransformableStruct tra;
	M3GNodeStruct          nod;
	M3GGroupStruct         grp;
	M3GWorldStruct         wld;
	grp.children_index_count = child_count;
	grp.children_index       = new unsigned int[child_count];
	for (int i = 0; i < (int)child_indices.size(); i++) {
        if (child_indices[i] > 0) {
    		grp.children_index[i] = child_indices[i];
        }
	}

	unsigned int object_index;

	wld.active_camera_index  = camera_index;
	wld.background_index     = background_index;
	writer->startObject        (M3G_TYPE_WORLD, &object_index);
	writer->writeObject3D      (obj);
	writer->writeTransformable (tra);
	writer->writeNode          (nod);
	writer->writeGroup         (grp);
	writer->writeWorld         (wld);
	writer->endObject          ();

   	Log:: print (" [%d] Write World : ",object_index);
	Log:: print ("  active_camera_index=%d", camera_index    );
	Log:: print (", background_index=%d"   , background_index);
	Log:: print (", child_indices=");
    for (int i = 0; i < (int)child_indices.size(); i++) {
        Log:: print ("%d,", child_indices[i]);
    }
    Log:: print ("\n");

	return object_index;
}


